package talayaco.gui;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import talayaco.enums.TipoAnimalEnum;
import talayaco.interfaz.GestionAnimalesINT;
import talayaco.interfaz.GestionCorralesINT;
import talayaco.utils.ErrorUtils;
import talayaco.utils.StringUtils;

public class MoverAnimales extends javax.swing.JInternalFrame {

	private static final long serialVersionUID = -126294036561647279L;
	/** Creates new form MoverAnimales */
	public MoverAnimales() {
		initComponents();
	}

	private void initComponents() {

		jPanel2 = new javax.swing.JPanel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		listadoTipoAnimal2 = new javax.swing.JComboBox();
		cantidad2 = new javax.swing.JTextField();
		listadoCorrales2 = new javax.swing.JComboBox();
		listadoCorrales3 = new javax.swing.JComboBox();
		botonMover = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();
		tipoAnimalMover = "";
		corralOrigen2 = "";
		corralDestino2 = "";

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setTitle("Mover Animales");

		try {
			listadoCorrales = GestionCorralesINT.getNombreCorrales();
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Mover Animales entre Corrales",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Tahoma", 0, 11))); // NOI18N

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 11));
		jLabel4.setText("Tipo de Animal");

		jLabel5.setFont(new java.awt.Font("Tahoma", 0, 11));
		jLabel5.setText("Cantidad");

		jLabel6.setFont(new java.awt.Font("Tahoma", 0, 11));
		jLabel6.setText("Corral de Origen");

		jLabel7.setFont(new java.awt.Font("Tahoma", 0, 11));
		jLabel7.setText("Corral de Destino");

		listadoTipoAnimal2.setFont(new java.awt.Font("Tahoma", 0, 11));
		listadoTipoAnimal2.setModel(new javax.swing.DefaultComboBoxModel(
				TipoAnimalEnum.getListadoTipos()));
		listadoTipoAnimal2.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				listadoTipoAnimal2ItemStateChanged(evt);
			}
		});

		cantidad2.setFont(new java.awt.Font("Tahoma", 0, 11));

		listadoCorrales2.setFont(new java.awt.Font("Tahoma", 0, 11));
		listadoCorrales2.setModel(new javax.swing.DefaultComboBoxModel(
				listadoCorrales));
		listadoCorrales2.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				listadoCorrales2ItemStateChanged(evt);
			}
		});

		listadoCorrales3.setFont(new java.awt.Font("Tahoma", 0, 11));
		listadoCorrales3.setModel(new javax.swing.DefaultComboBoxModel(
				listadoCorrales));
		listadoCorrales3.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				listadoCorrales3ItemStateChanged(evt);
			}
		});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																false)
														.addComponent(
																listadoTipoAnimal2,
																javax.swing.GroupLayout.Alignment.LEADING,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jLabel4,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(cantidad2)
														.addComponent(
																jLabel5,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																listadoCorrales2,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(jLabel6))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																listadoCorrales3,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(jLabel7))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel6)
																						.addComponent(
																								jLabel7))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								listadoCorrales2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								listadoCorrales3,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								jLabel5))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								listadoTipoAnimal2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								cantidad2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								21,
																								javax.swing.GroupLayout.PREFERRED_SIZE))))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		botonMover.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		botonMover.setText("Aceptar");
		botonMover.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				botonMoverActionPerformed(evt);
			}
		});

		jButton1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jButton1.setText("Cancelar");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jButton1)
																.addGap(18, 18,
																		18)
																.addComponent(
																		botonMover))
												.addComponent(
														jPanel2,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														botonMover,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														21,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														jButton1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														21,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	private void listadoTipoAnimal2ItemStateChanged(java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un tipo")
				tipoAnimalMover = "";
			else {
				tipoAnimalMover = evt.getItem().toString();
			}
		}
	}

	private void listadoCorrales2ItemStateChanged(java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un Tipo")
				corralOrigen2 = "";
			else {
				corralOrigen2 = evt.getItem().toString();
			}
		}
	}

	private void listadoCorrales3ItemStateChanged(java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un Tipo")
				corralDestino2 = "";
			else {
				corralDestino2 = evt.getItem().toString();
			}
		}
	}

	private void botonMoverActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			if (StringUtils.isNullOrBlank(tipoAnimalMover)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_ANIMAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else if (StringUtils.isNullOrBlank(cantidad2.getText())) {
				JOptionPane.showMessageDialog(this, ErrorUtils.CANTIDAD_VACIA,
						"ERROR", JOptionPane.ERROR_MESSAGE);
			} else if (!StringUtils.isNumber(cantidad2.getText())) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.CANTIDAD_INVALIDA, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else if (StringUtils.isNullOrBlank(corralOrigen2)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_CORRAL_ORIGEN, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else if (StringUtils.isNullOrBlank(corralDestino2)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_CORRAL_DESTINO, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else if (corralOrigen2.equals(corralDestino2)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.CORRALES_IGUALES, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else {

				int resultado;
				resultado = GestionAnimalesINT.cantidadAnimalesByTipoyCorral(
						Integer.parseInt(cantidad2.getText()), tipoAnimalMover,
						corralOrigen2);

				if (resultado != -1) {
					JOptionPane.showMessageDialog(this,
							ErrorUtils.CANTIDAD_CORRAL_MENOR_1 + corralOrigen2
									+ ErrorUtils.CANTIDAD_CORRAL_MENOR_2
									+ resultado + " " + tipoAnimalMover,
							"ERROR", JOptionPane.ERROR_MESSAGE);
				} else {

					Map<String, String> animales = new HashMap<String, String>();
					animales.put("tipoAnimal", tipoAnimalMover);
					animales.put("cantidad", cantidad2.getText());
					animales.put("corralOrigen", corralOrigen2);
					animales.put("corralDestino", corralDestino2);


					GestionCorralesINT.moverAnimales(animales, this);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		if (JOptionPane.showConfirmDialog(this, ErrorUtils.CANCELAR,
				"CANCELAR", JOptionPane.YES_NO_OPTION) == 0)
			this.dispose();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new MoverAnimales().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton botonMover;
	private javax.swing.JTextField cantidad2;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JComboBox listadoCorrales2;
	private javax.swing.JComboBox listadoCorrales3;
	private javax.swing.JComboBox listadoTipoAnimal2;
	private String tipoAnimalMover;
	private String corralOrigen2;
	private String corralDestino2;
	private String[] listadoCorrales;
	// End of variables declaration
}
